<template>
  <el-dialog title="关联角色" :visible.sync="showRoleDialog">
    <!-- el-checkbox-group选中的是 当前用户所拥有的角色  需要绑定 当前用户拥有的角色-->
    <el-checkbox-group v-model="checkList">
      <el-checkbox
        v-for="item in allRoleList"
        :key="item.id "
        :label="item.id"
      >
        {{ item.name }}
      </el-checkbox>
    </el-checkbox-group>
    <el-row slot="footer" type="flex" justify="center">
      <el-col :span="6">
        <el-button type="primary" size="small" @click="submit">确定</el-button>
        <el-button size="small" @click="handleClose">取消</el-button>
      </el-col>
    </el-row>
  </el-dialog>
</template>

<script>
import { getRoleList } from '@/api/setting'
import { userInfoById, setUserRole } from '@/api/employees'
export default {
  data() {
    return {
      showRoleDialog: false,
      allRoleList: [], // 角色列表
      checkList: [], // 复选框列表
      userId: ''// 员工ID
    }
  },
  created() {
    this.getAllRoleList()
  },
  methods: {
    // 提交事件
    async submit() {
      await setUserRole({
        id: this.userId, // 提交员工ID
        roleIds: this.checkList// 提交复选框选中的角色ID
      })
      this.$message.success('操作成功')
      this.showRoleDialog = false
    },
    // 获取当前员工信息
    async getUserRoleInfo(id) {
      // 将员工id缓存下来
      this.userId = id
      const res = await userInfoById(id)
      this.checkList = res.roleIds || []// 复选框列表赋值
    },
    // 获取列表
    async getAllRoleList() {
      const { rows } = await getRoleList({
        page: 1,
        pageseize: 1000
      })
      this.allRoleList = rows
    },
    // 关闭弹窗
    handleClose() {
      this.showRoleDialog = false
    }
  }
}
</script>
